package work.xiaohong.mybatisplus.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import com.baomidou.mybatisplus.core.toolkit.AES;

import java.util.Date;

/**
 * 自定义填充处理
 */
@Component // 一定不要忘记把处理器加到IOC容器中！
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "gmtCreate", Date.class, new Date());
        this.strictInsertFill(metaObject, "gmtModified", Date.class, new Date());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "gmtModified", Date.class, new Date());
    }

    /**
     * 数据库密码加密
     * @param args
     */
    public static void main(String[] args) {
        // 生成16位的随机的密钥
        String key = AES.generateRandomKey();

        String mysql = AES.encrypt("jdbc:mysql://localhost:3070/xiaohong_chat?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false&allowPublicKeyRetrieval=true", key);
        String username = AES.encrypt("root", key);
        String password = AES.encrypt("xiaohongsql", key);

        System.out.println("密钥为---->--mpw.key="+key);
        System.out.println("mysql加密---->"+"mpw:"+mysql);
        System.out.println("用户名加密---->"+"mpw:"+username);
        System.out.println("密码加密---->"+"mpw:"+password);

    }

}
